package com.itheima.takeout.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.itheima.takeout.R;
import com.itheima.takeout.model.db.bean.ReceiptAddressBean;
import com.itheima.takeout.ui.activity.AddEditReceiptAddressActivity;

import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by Apple on 2016/9/5.
 */
public class ReceiptAddressAdapter extends RecyclerView.Adapter {

    private Context context;
    private List<ReceiptAddressBean> receiptAddressBeanList;

    public ReceiptAddressAdapter(Context context, List<ReceiptAddressBean> receiptAddressBeanList) {
        this.context = context;
        this.receiptAddressBeanList = receiptAddressBeanList;
    }

    public void setReceiptAddressBeanList(List<ReceiptAddressBean> receiptAddressBeanList) {
        this.receiptAddressBeanList = receiptAddressBeanList;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_receipt_address, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ReceiptAddressBean bean = receiptAddressBeanList.get(position);
        ViewHolder viewHolder = (ViewHolder) holder;
        viewHolder.tvName.setText(bean.getName());
        viewHolder.tvSex.setText(bean.getSex());
        if(TextUtils.isEmpty(bean.getPhoneOther())){
            viewHolder.tvPhone.setText(bean.getPhone());
        }else{
            viewHolder.tvPhone.setText(bean.getPhone()+","+bean.getPhoneOther());
        }

        if(!TextUtils.isEmpty(bean.getLabel())){
            viewHolder.tvLabel.setVisibility(View.VISIBLE);
            int index = getlabelIndex(bean.getLabel());
            viewHolder.tvLabel.setText(addressLabels[index]);
            viewHolder.tvLabel.setBackgroundColor(bgLabels[index]);
        }else{
            viewHolder.tvLabel.setVisibility(View.GONE);
        }

        viewHolder.tvAddress.setText(bean.getReceiptAddress()+bean.getDetailAddress());

        viewHolder.setPosition(position);

    }

    private int getlabelIndex(String label){
        int index = 0;
        for (int i = 0; i < addressLabels.length; i++) {
            if(label.equals(addressLabels[i])){
                index = i;
                break;
            }
        }
        return index;
    }

    String[] addressLabels = new String[]{ "家", "公司", "学校"};
    int[] bgLabels = new int[]{
            Color.parseColor("#fc7251"),//家  橙色
            Color.parseColor("#468ade"),//公司 蓝色
            Color.parseColor("#02c14b"),//学校   绿色

    };
    @Override
    public int getItemCount() {
        return receiptAddressBeanList != null?receiptAddressBeanList.size():0;
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        @InjectView(R.id.tv_name)
        TextView tvName;
        @InjectView(R.id.tv_sex)
        TextView tvSex;
        @InjectView(R.id.tv_phone)
        TextView tvPhone;
        @InjectView(R.id.tv_label)
        TextView tvLabel;
        @InjectView(R.id.tv_address)
        TextView tvAddress;
        @InjectView(R.id.iv_edit)
        ImageView ivEdit;

        ViewHolder(View view) {
            super(view);
            ButterKnife.inject(this, view);
        }

        private int position;
        public void setPosition(int position){
            this.position = position;
        }

        @OnClick(R.id.iv_edit)
        public void onClick(View view){
            ReceiptAddressBean receiptAddressBean = receiptAddressBeanList.get(position);
            Intent intent = new Intent(context, AddEditReceiptAddressActivity.class);
            intent.putExtra("receiptAddressBean",receiptAddressBean);
            context.startActivity(intent);
        }
    }
}
